home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / SourceCode / MiscKit1.7.1 / MiscKit / Palettes / MiscSwapKitPalette / MiscSwapKit.subproj / MiscSwapContentsController.m < prev    next >
Encoding:
Text File  |  1995-04-12  |  7.1 KB  |  311 lines

  1. /* MiscSwapContentsController.m                 
  2.  *
  3.  * A very simple class for controlling swapAble views. A subclass is a must 
  4.  * to easily work with the MiscSwapView classes.
  5.  *
  6.  * For more interface-info see the header file. In depth information
  7.  * can be found here in the source-code.
  8.  *
  9.  * Written by:         Thomas Engel
  10.  * Created:            24.01.1994 (Copyleft)
  11.  * Last Modified:     25.09.1994
  12.  * Copyright (C) 1995 Thomas Engel
  13.  */
  14.  
  15. #import <misckit/MiscSwapView.h>
  16. #import <misckit/MiscSwapContentsController.h>
  17.  
  18. // Defined in order to do archiving and versioning properly, so
  19. // if the archiving changes we will be able to read all versions.
  20.  
  21. #define MISC_SCC_VERSION 0
  22. #define MISC_SCC_CLASSNAME "MiscSwapContentsController"
  23.  
  24.  
  25. // Declarations of private methods that need not be in the
  26. // header file.
  27.  
  28. @interface MiscSwapContentsController (PrivateMethods)
  29.  
  30. - _dispatchToDelegate: (SEL)message;
  31. - _dispatchToDelegate: (SEL)message with: anObject;
  32.  
  33. @end
  34.  
  35.  
  36. @implementation MiscSwapContentsController
  37.  
  38. + initialize
  39. {
  40.     // Sets the version of this class for archiving purposes.
  41.     
  42.     if (self == [MiscSwapContentsController class])
  43.         [self setVersion: MISC_SCC_VERSION];
  44.         
  45.     return self;
  46. }
  47.  
  48. - init
  49. {
  50.     [super init];
  51.     
  52.     swapView = nil;
  53.     view = nil;
  54.     trigger = nil;
  55.     triggerTag = 0;
  56.     delegate = nil;
  57.     
  58.     return self;
  59. }
  60.  
  61. - setSwapView:aView
  62. {
  63.     // Here we set our swapView. This objects is the right place to
  64.     // register ourselves for the swapping. But only if we have a trigger..
  65.     // otherwise we have to wait for a awakeFromNib message.
  66.     // Sorry we can only register for one swapView at a time. So if we had
  67.     // a swapView before..lets say good bye.
  68.     
  69.     if( swapView ) [swapView removeController:self];
  70.     
  71.     swapView = aView;
  72.     
  73.     // I removed the necessity that the trigger has to be set to something
  74.     // before adding ourselves to the swapview's list of controllers. The
  75.     // swapView class was also changed so it would treat controllers with
  76.     // no trigger in the correct way, since you can depend upon tags and
  77.     // not use any controllers. 
  78.     // You also do not need awakeFromNib anymore.
  79.     
  80.     [swapView addController: self];
  81.         
  82.     return self;
  83. }
  84.  
  85. - swapView
  86. {
  87.     if (swapView != nil)
  88.         return swapView;
  89.     
  90.     // If we aren't pointing to a swapview maybe our delegate
  91.     // knows something we don't. I don't really know why you
  92.     // would have the delegate know the swapView, but it is 
  93.     // here for consistency.    
  94.     return [self _dispatchToDelegate: @selector(swapView)];
  95.         
  96. }
  97.  
  98. - setView:aView
  99. {
  100.     view = aView;
  101.     return self;
  102. }
  103.  
  104. - view
  105. {
  106.     if (view != nil)
  107.         return view;
  108.     
  109.     // This way you can have views that are all in seperate nibs and
  110.     // have the delegate get them when they are needed.
  111.     return [self _dispatchToDelegate: @selector(view)];
  112. }
  113.  
  114. - setTrigger:anObject
  115. {
  116.     // The trigger is the object we are related to. By default we try to set
  117.     // the triggerTag according to that object.
  118.     // Activating the trigger object (or an object with this tag) will cause
  119.     // us to swap in.
  120.     
  121.     trigger = anObject;
  122.     
  123.     if( [trigger respondsTo:@selector(tag)] )
  124.             [self setTriggerTag:[trigger tag]];
  125.     else    [self setTriggerTag:0];
  126.             
  127.     return self;
  128. }
  129.  
  130. - trigger
  131. {
  132.     return trigger;
  133. }
  134.  
  135. - setTriggerTag:(int)tag
  136. {
  137.     // Sets the tag we will be activated for.
  138.     // Working with tags frees us from having to know what typ of object caused
  139.     // the action (TextCell,ButtonCell,Matrix or what ever..) as long as the
  140.     // tags are handled the right way.
  141.     
  142.     triggerTag = tag;
  143.     return self;
  144. }
  145.  
  146. - (int)triggerTag
  147. {
  148.     return triggerTag;
  149. }
  150.  
  151. // As an alternative to subclassing this controller to "control" the rest of
  152. // the UI objects on the view, you can make it a seperate class and connect
  153. // it to the delegate, so it knows when it will be swapped in and out.
  154.  
  155. - delegate
  156. {
  157.     return delegate;
  158. }
  159.  
  160. - setDelegate: aDelegate
  161. {
  162.     delegate = aDelegate;
  163.     return self;
  164. }
  165.  
  166.  
  167. /*
  168.  * These revert/ok msg are send to setup/save the contents of a view.
  169.  * Only the revert msg is invoked by default. If you have to store some
  170.  * data when swapping out implement a [self ok:self] msg iside willSwapOut.
  171.  * Ok should be used to save the changes made and revert should init the
  172.  * view to show the current settings.
  173.  */
  174.  
  175. - ok:sender
  176. {
  177.     [self _dispatchToDelegate: @selector(ok:) with: sender];
  178.     return self;
  179. }
  180.  
  181. - revert:sender
  182. {
  183.     [self _dispatchToDelegate: @selector(revert:) with: sender];
  184.     return self;
  185. }
  186.  
  187. /*
  188.  * These messages we will get from our swapView. That´s how we can
  189.  * recognize that maybe some things have to be written to the defaults
  190.  * database or something has to be updated etc.
  191.  * You should override them in you subclass.
  192.  * They are no actionMethods because we always know who our swapCtrl. is.
  193.  * So sender is not needed here.
  194.  */
  195.  
  196. - willSwapIn
  197. {
  198.     [self revert:self];
  199.     [self _dispatchToDelegate: @selector(willSwapViewIn:) with: self];
  200.     return self;
  201. }
  202.  
  203. - willSwapOut
  204. {
  205.     [self _dispatchToDelegate: @selector(willSwapViewOut:) with: self];
  206.     return self;
  207. }
  208.  
  209. - didSwapIn
  210. {
  211.     [self _dispatchToDelegate: @selector(didSwapViewIn:) with: self];
  212.     return self;
  213. }
  214.  
  215. - didSwapOut
  216. {
  217.     [self _dispatchToDelegate: @selector(didSwapViewOut:) with: self];
  218.     return self;
  219. }
  220.  
  221.  
  222.  
  223. - read: (NXTypedStream *)stream
  224. {
  225.   int  version;
  226.   
  227.     [super read: stream];
  228.     version = NXTypedStreamClassVersion(stream, MISC_SCC_CLASSNAME);
  229.     
  230.     switch (version)
  231.     {
  232.         case 0:
  233.             swapView = NXReadObject (stream);
  234.             view = NXReadObject (stream);
  235.             trigger = NXReadObject (stream);
  236.             delegate = NXReadObject (stream);
  237.             NXReadType (stream, "i", &triggerTag);
  238.             break;
  239.         default:
  240.             break;
  241.      }
  242.           
  243.     return self;
  244. }
  245.  
  246. - write: (NXTypedStream *)stream
  247. {
  248.     [super write: stream];
  249.     NXWriteObjectReference (stream, swapView);
  250.     NXWriteObjectReference (stream, view);
  251.     NXWriteObjectReference (stream, trigger);
  252.     NXWriteObjectReference (stream, delegate);
  253.     NXWriteType (stream, "i", &triggerTag);
  254.  
  255.     return self;
  256. }
  257.  
  258. @end
  259.  
  260.  
  261. @implementation MiscSwapContentsController (PrivateMethods)
  262.  
  263. - _dispatchToDelegate: (SEL)message
  264. {
  265.     if (delegate && [delegate respondsTo: message])
  266.         return [delegate perform: message];
  267.     
  268.     // No delegate or doesn't respond to message.    
  269.     return nil;
  270. }
  271.  
  272.  
  273.  
  274. - _dispatchToDelegate: (SEL)message with: anObject
  275. {    
  276.     if (delegate && [delegate respondsTo: message])
  277.         return [delegate perform: message with: anObject];
  278.     
  279.     return nil;
  280. }
  281.  
  282. @end
  283.  
  284.         
  285. /*
  286.  * History: 25.09.94 Added archiving (read:, write:, +initialize).
  287.  *                     Added a delegate so this class could be on a palette
  288.  *                     and would not require subclassing. Also took away
  289.  *                     the awakeFromNib method since you can now register a
  290.  *                     controller without having a trigger set.
  291.  *
  292.  *            14.02.94 Changed the classes name to MiscSwapContentsController
  293.  *                     from the MiscSwapSubviewController becauses it fits better
  294.  *                     to what it really is.
  295.  *
  296.  *            24.01.94 Made it a Misc object and changed it to work with the
  297.  *                     new Misc stuff.
  298.  *
  299.  *            09.01.94 Added the ok/revert stuff.
  300.  *
  301.  *            08.01.94 Derived from my old swapViewdelegate. The code+methods
  302.  *                     were cleaned up a bit.
  303.  *
  304.  *
  305.  * Bugs: - no read/write; (not anymore)
  306.  *
  307.  *         - Maybe I should do more responds to or class checking.. hmm ??
  308.  *
  309.  *         - Not a bug but: I don't love the double registry made by awakeFromNib
  310.  *           and setSwapView. It works but it's not elegant. (not anymore)
  311.  */